package com.shigu.trade.service;

import com.opentae.data.mall.beans.*;
import com.opentae.data.mall.interfaces.*;
import com.opentae.data.mall.pojo.AddrData;
import com.shigu.MallResponse;
import com.shigu.address.bo.UserAddressBo;
import com.shigu.address.service.AddressAdminService;
import com.shigu.address.service.AddressService;
import com.shigu.address.vo.AddressResponse;
import com.shigu.postFee.service.PostFeeService;
import com.shigu.trade.enums.TradeStatus;
import com.shigu.trade.vo.PayByWxResponse;
import com.shigu.trade.vo.PostFeeResponse;
import com.shigu.util.MoneyUtil;
import com.shigu.weixin.enums.WxConfigSource;
import com.shigu.weixin.service.PayApplyService;
import com.shigu.weixin.vo.JSAPIVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/*
 * create by  过儿  2019-05-15
 * H5 页面 处理订单 服务层
 *
 * 因为不是通过token 交互，所以去session 中获取userId
 */
@Service
public class PageTradeService {
    @Autowired
    PostFeeService postFeeService;
    @Autowired
    MallOrderMapper mallOrderMapper;
    @Autowired
    MallItemMapper mallItemMapper;
    @Autowired
    MallTradeMapper mallTradeMapper;
    @Autowired
    MallOrderLogisticsMapper mallOrderLogisticsMapper;
    @Autowired
    PayApplyService payApplyService;
    @Autowired
    MallAreaMapper mallAreaMapper;
    @Autowired
    AddressAdminService addressAdminService;
    @Autowired
    AddressService addressService;

    /**
     * @description h5 页面获取运费
     * @author 郭尔
     * @date 2019-05-15 18:13
     */
    public PostFeeResponse getH5TradePostFee(String provId, String tradeCode) {
        Long id = Long.valueOf(provId);
        MallOrder order = mallOrderMapper.selectOrderByTradeCode(tradeCode);
        MallItem item = mallItemMapper.selectByPrimaryKey(order.getItemId());
        int weight = item.getWeight() == null ? 0 : item.getWeight();
        double fee = postFeeService.postFee(weight, item.getFeeId(), order.getNum(), id);
        return new PostFeeResponse(MoneyUtil.formatPriceMoney(fee));
    }

    /**
     * @description
     * @author 郭尔
     * @date 2019-05-15 18:15
     */
    public synchronized MallResponse compeleteH5OrderInfo(UserAddressBo bo, String tradeCode, Long userId) throws Exception {
        addressAdminService.addAddress(userId,bo);
        MallTrade trade = mallTradeMapper.selByTradeCode(tradeCode);
        MallOrder order = mallOrderMapper.selectOrderByTradeCode(tradeCode);
        MallItem item = mallItemMapper.selectByPrimaryKey(order.getItemId());

        int weight = item.getWeight()==null?0:item.getWeight();
        double postFee = postFeeService.postFee(weight, item.getFeeId(), order.getNum(), bo.getProvId());//获取运费
        order.setPostFee(postFee);
        order.setPayMoney(order.getPayMoney());

        trade.setTradeStatus(TradeStatus.COMFIRM.getValue());
        trade.setTotalMoney(trade.getPayMoney()+postFee);

        AddrData addr = mallAreaMapper.selAddr(bo.getAreaId());

        MallOrderLogistics logistics = new MallOrderLogistics();
        logistics.setReceiveTelephone(bo.getReceiveTelephone());
        logistics.setReceiveName(bo.getReceiveName());
        logistics.setAddressDetail(bo.getReceiveAddress());
        logistics.setTradeCode(tradeCode);
        logistics.setTid(trade.getTid());
        logistics.setProvName(addr.getProvName());
        logistics.setAreaName(addr.getAreaName());
        logistics.setCityName(addr.getCityName());
        logistics.setBuyerId(userId);

        mallOrderLogisticsMapper.insertSelective(logistics);
        mallOrderMapper.updateByPrimaryKeySelective(order);
        mallTradeMapper.updateByPrimaryKeySelective(trade);
        return new MallResponse().success();
   }

    /**
     * @description
     * @author 郭尔
     * @date 2019-05-16 10:28
     */
    public synchronized AddressResponse getAddressInfo(Long userId){
        return addressService.myDefaultAddress(userId);
    }

    /**
     *
     * @description
     * @author 郭尔
     * @date 2019-05-16 13:48
     */
    public PayByWxResponse getWxPayInfo(String tradeCode,Long userId) throws Exception {
        MallTrade trade = mallTradeMapper.selByTradeCode(tradeCode);
        JSAPIVo vo = payApplyService.applyWxPay(userId, trade.getTotalMoney(), tradeCode, WxConfigSource.GZH);
        PayByWxResponse response = new PayByWxResponse();
        response.setAppId(vo.getAppId());
        response.setTimeStamp(vo.getTimeStamp());
        response.setNonceStr(vo.getNonceStr());
        response.setPrepayId(vo.getPrepayId());
        response.setPaySign(vo.getPaySign());
        return response.success();
    }

}
